package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.context.BaseContext;
import com.itheima.advantagesup.dto.UserDataListDTO;
import com.itheima.advantagesup.dto.UserUpdateDTO;
import com.itheima.advantagesup.dto.UserUpdateInfoDTO;
import com.itheima.advantagesup.entity.YddUser;
import com.itheima.advantagesup.mapper.UserMapper;
import com.itheima.advantagesup.service.UserService;
import com.itheima.advantagesup.vo.UserDataListVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public void update(UserUpdateInfoDTO userUpdateInfoDTO) {
        YddUser yddUser = new YddUser();
        BeanUtils.copyProperties(userUpdateInfoDTO, yddUser);
        yddUser.setId(Integer.parseInt(userUpdateInfoDTO.getId()));
        userMapper.update(yddUser);
    }

    @Override
    public PageBean selectDataList(UserDataListDTO userDataListDTO) {
        Integer userId = Integer.valueOf(BaseContext.getCurrentId().toString());
        PageHelper.startPage(Integer.parseInt(userDataListDTO.getPage()), Integer.parseInt(userDataListDTO.getLimit()));
        com.github.pagehelper.Page<UserDataListVO> page = userMapper.findByUserData(userId, userDataListDTO.getType());
        PageBean pageBean= PageBean.builder()
                .count((int)page.getTotal())
                .currentPage(Integer.parseInt(userDataListDTO.getPage()))
                .pageSize( page.getPageSize())
                .totalPages(page.getPages())
                .build();
        List<UserDataListVO> result = page.getResult();
        pageBean.setData(result);
        return pageBean;
    }
}

